//
//  ViewController3.m
//  20101
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController3.h"
#import "StudentModel.h"
@interface ViewController3 ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray *stuList;

@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController3

- (NSMutableArray *)stuList
{
    if(_stuList == nil)
    {
        NSFileManager *fm = [NSFileManager defaultManager];
        if([fm fileExistsAtPath:[self dataPath]])
        {
            NSData *data = [NSData dataWithContentsOfFile:[self dataPath]];
            NSKeyedUnarchiver *unArch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            _stuList = [unArch decodeObjectForKey:@"Students"];
            [unArch finishDecoding];
            
        }
        else
        {
            _stuList = [NSMutableArray array];
        }
    }
    return _stuList;
}
- (NSString *)dataPath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"students.arch"];
    return filePath;
}
- (void)saveData
{
    NSMutableData *mData = [NSMutableData data];
    NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
    [arch encodeObject:self.stuList forKey:@"Students"];
    [arch finishEncoding];
    [mData writeToFile:[self dataPath] atomically:YES];
}


- (IBAction)add:(id)sender
{
    StudentModel *m = [[StudentModel alloc] initWithId:self.idTextField.text
                                               andName:self.nameTextField.text
                                                andAge:self.ageTextField.text];
    [self.stuList addObject:m];
    [self saveData];
    [self reload];
}

- (IBAction)remove:(id)sender
{
    for(StudentModel *m in self.stuList)
    {
        if([m.stuId isEqualToString:self.idTextField.text])
        {
            [self.stuList removeObject:m];
            break;
        }
    }
    [self saveData];
    [self reload];
}
- (IBAction)modify:(id)sender
{
    for(StudentModel *m in self.stuList)
    {
        if([m.stuId isEqualToString:self.idTextField.text])
        {
            m.stuName = self.nameTextField.text;
            m.stuAge = self.ageTextField.text;
            break;
        }
    }
    [self saveData];
    [self reload];
}

- (IBAction)find:(id)sender
{
    for(StudentModel *m in self.stuList)
    {
        if([m.stuId isEqualToString:self.idTextField.text])
        {
            self.nameTextField.text = m.stuName;
            self.ageTextField.text = m.stuAge;
            break;
        }
    }
}

- (IBAction)findAll:(id)sender
{
    for (StudentModel *m in self.stuList)
    {
        NSLog(@"%@,%@,%@",m.stuId,m.stuName,m.stuAge);
    }
}

-(void)reload
{
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.stuList.count;
}


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
     StudentModel *s=self.stuList[indexPath.row];
     cell.textLabel.text=[NSString stringWithFormat:@"ID:%@,姓名:%@,年龄:%@",s.stuId,s.stuName,s.stuAge];
 
 return cell;
 }
 

@end
